package p2.code;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class InterruptLockStudy {

    private static Integer num1 = 0;

    //创建锁
    private static final Lock lock = new ReentrantLock();

    /**
     * 内置锁修饰普通方法锁住对象方法
     * @param num
     */
    public synchronized void add(int num) throws InterruptedException {
        num1 += num;
        Thread.sleep(10000);
    }

    public synchronized void sub(int num){
        num1 -= num;
    }

    public void add2(int num) throws InterruptedException {
        if(lock.tryLock(2, TimeUnit.SECONDS)){
            try {
                num1 += num;
                Thread.sleep(10000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            finally {
                lock.unlock();
            }
        }else{
            System.out.println("加操作获取锁失败！！");
        }
    }

    public void sub2(int num) throws InterruptedException {
        if(lock.tryLock(2, TimeUnit.SECONDS)){
            try {
                num1 -= num;
                Thread.sleep(10000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            finally {
                lock.unlock();
            }
        }else{
            System.out.println("减操作获取锁失败！！");
        }
    }

    public static void main(String[] args) {
        InterruptLockStudy study = new InterruptLockStudy();
        new Thread(()->{
            try {
                System.out.println("不可中断锁加操作执行开始");
                long start = System.currentTimeMillis();
                study.add(1);
                System.out.println("不可中断锁加操作执行结束,耗时 "+(System.currentTimeMillis()-start)+" ms");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{
            System.out.println("不可中断锁减操作执行开始");
            long start = System.currentTimeMillis();
            study.sub(1);
            System.out.println("不可中断锁减操作执行完毕,耗时 "+(System.currentTimeMillis()-start)+" ms");
        }).start();

        InterruptLockStudy study1 = new InterruptLockStudy();
        new Thread(()->{
            try {
                System.out.println("可中断锁加操作执行开始");
                long start = System.currentTimeMillis();
                study1.add2(1);
                System.out.println("可中断锁加操作执行结束,耗时 "+(System.currentTimeMillis()-start)+" ms");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()->{
            try {
                System.out.println("可中断锁减操作执行开始");
                long start = System.currentTimeMillis();
                study1.sub2(1);
                System.out.println("可中断锁加减操作执行结束,耗时 "+(System.currentTimeMillis()-start)+" ms");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }


}
